# -*- coding: utf-8 -*-
import re
import os
import requests
import json
import re
from pyquery import PyQuery as pq


def findPart(regex, text, name):
    a = u"这是个中文"
    b = re.compile(u"[\u4e00-\u9fa5]{1,2}")
    # res = b.findall(text)
    res = re.findall(regex, text)
    if res:
        print("There are %d %s parts:\n" % (len(res), name))
    for r in res:
        print("\t", r.encode("utf8"))


# text = "#who#helloworld#a中文x#".encode('utf-8').decode('utf-8')
# usample = text.encode('utf8')
# findPart(u"[\u4e00-\u9fa5]+", text, "unicode chinese")
#
data_url = "http://live.aicai.com/jsbf/timelyscore!dynamicBfDataFromPage.htm?lotteryType=sfc&issue=17147"
response = requests.get(data_url)
print(response.text)
data = json.loads(response.text)
print(data)
bf_page = pq(data['result']['bf_page'])
last_score = bf_page('span')
# print(bf_page('#jq_odds_home_1').text())
pattern = re.compile(r'\d{1,2}:\d{1,2}')
jq_price_home = 'jq_price_home_'  # 必发价位胜
jq_index_home = 'jq_index_home_'  # 必发指数胜
jq_odds_home = 'jq_odds_home_'  # 百家欧赔胜
jq_price_draw = 'jq_price_draw_'  # 必发价位平
jq_index_draw = 'jq_index_draw_'  # 必发指数平
jq_odds_draw = 'jq_odds_draw_'  # 百家欧赔平
jq_price_away = 'jq_price_away_'  # 必发价位负
jq_index_away = 'jq_index_away_'  # 必发指数负
jq_odds_away = 'jq_odds_away_'  # 百家欧赔负
index = 0
for i in range(last_score.size()):
    score = last_score.eq(i)
    # print(score)
    match = pattern.match(score.text())
    if match:
        print(match.group())
        price_home = bf_page('#%s%d' % (jq_price_home, index)).text()
        price_draw = bf_page('#%s%d' % (jq_price_draw, index)).text()
        price_away = bf_page('#%s%d' % (jq_price_away, index)).text()
        index_home = bf_page('#%s%d' % (jq_index_home, index)).text()
        index_draw = bf_page('#%s%d' % (jq_index_draw, index)).text()
        index_away = bf_page('#%s%d' % (jq_index_away, index)).text()
        odds_home = bf_page('#%s%d' % (jq_odds_home, index)).text()
        odds_draw = bf_page('#%s%d' % (jq_odds_draw, index)).text()
        odds_away = bf_page('#%s%d' % (jq_odds_away, index)).text()
        print("%s\t%s\t%s\n%s\t%s\t%s\n%s\t%s\t%s\n"
              % (price_home, index_home, odds_home,
                 price_draw, index_draw, odds_draw,
                 price_away, index_away, odds_away))
        index += 1
